Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.13% covered (warning)
87.13%
88 / 101
86.67% covered (warning)
86.67%
13 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
EvolutionApiServiceProvider
87.13% covered (warning)
87.13%
88 / 101
86.67% covered (warning)
86.67%
13 / 15
18.69
0.00% covered (danger)
0.00%
0 / 1
 register
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 boot
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 registerConnectionManager
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 registerRateLimiter
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 registerLogger
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 registerMetrics
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 registerClient
33.33% covered (danger)
33.33%
4 / 12
0.00% covered (danger)
0.00%
0 / 1
1.30
 registerService
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
1.24
 registerWebhookProcessor
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 publishConfig
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 publishMigrations
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 registerCommands
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 registerRoutes
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 registerEventListeners
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 provides
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Lynkbyte\EvolutionApi;
6
7use Illuminate\Support\ServiceProvider;
8use Lynkbyte\EvolutionApi\Client\ConnectionManager;
9use Lynkbyte\EvolutionApi\Client\EvolutionClient;
10use Lynkbyte\EvolutionApi\Client\RateLimiter;
11use Lynkbyte\EvolutionApi\Console\Commands\HealthCheckCommand;
12use Lynkbyte\EvolutionApi\Console\Commands\InstallCommand;
13use Lynkbyte\EvolutionApi\Console\Commands\InstanceStatusCommand;
14use Lynkbyte\EvolutionApi\Console\Commands\PruneOldDataCommand;
15use Lynkbyte\EvolutionApi\Console\Commands\RetryFailedMessagesCommand;
16use Lynkbyte\EvolutionApi\Contracts\EvolutionClientInterface;
17use Lynkbyte\EvolutionApi\Contracts\RateLimiterInterface;
18use Lynkbyte\EvolutionApi\Logging\EvolutionApiLogger;
19use Lynkbyte\EvolutionApi\Metrics\MetricsCollector;
20use Lynkbyte\EvolutionApi\Services\EvolutionService;
21use Lynkbyte\EvolutionApi\Webhooks\WebhookProcessor;
22
23class EvolutionApiServiceProvider extends ServiceProvider
24{
25    /**
26     * Register any application services.
27     */
28    public function register(): void
29    {
30        $this->mergeConfigFrom(
31            __DIR__.'/../config/evolution-api.php',
32            'evolution-api'
33        );
34
35        $this->registerConnectionManager();
36        $this->registerRateLimiter();
37        $this->registerLogger();
38        $this->registerMetrics();
39        $this->registerClient();
40        $this->registerService();
41        $this->registerWebhookProcessor();
42    }
43
44    /**
45     * Bootstrap any application services.
46     */
47    public function boot(): void
48    {
49        $this->publishConfig();
50        $this->publishMigrations();
51        $this->registerCommands();
52        $this->registerRoutes();
53        $this->registerEventListeners();
54    }
55
56    /**
57     * Register the connection manager.
58     */
59    protected function registerConnectionManager(): void
60    {
61        $this->app->singleton(ConnectionManager::class, function ($app) {
62            return new ConnectionManager(
63                config('evolution-api.connections', []),
64                config('evolution-api.server_url'),
65                config('evolution-api.api_key')
66            );
67        });
68    }
69
70    /**
71     * Register the rate limiter.
72     */
73    protected function registerRateLimiter(): void
74    {
75        $this->app->singleton(RateLimiterInterface::class, function ($app) {
76            return new RateLimiter(
77                $app['cache']->store(config('evolution-api.rate_limiting.driver')),
78                config('evolution-api.rate_limiting', [])
79            );
80        });
81
82        $this->app->alias(RateLimiterInterface::class, RateLimiter::class);
83    }
84
85    /**
86     * Register the logger.
87     */
88    protected function registerLogger(): void
89    {
90        $this->app->singleton(EvolutionApiLogger::class, function ($app) {
91            return new EvolutionApiLogger(
92                $app['log'],
93                config('evolution-api.logging', [])
94            );
95        });
96    }
97
98    /**
99     * Register the metrics collector.
100     */
101    protected function registerMetrics(): void
102    {
103        $this->app->singleton(MetricsCollector::class, function ($app) {
104            return new MetricsCollector(
105                config('evolution-api.metrics', [])
106            );
107        });
108    }
109
110    /**
111     * Register the Evolution API client.
112     */
113    protected function registerClient(): void
114    {
115        $this->app->singleton(EvolutionClientInterface::class, function ($app) {
116            return new EvolutionClient(
117                $app->make(ConnectionManager::class),
118                $app->make(RateLimiterInterface::class),
119                $app->make(EvolutionApiLogger::class),
120                $app->make(MetricsCollector::class),
121                config('evolution-api.http', []),
122                config('evolution-api.retry', [])
123            );
124        });
125
126        $this->app->alias(EvolutionClientInterface::class, EvolutionClient::class);
127        $this->app->alias(EvolutionClientInterface::class, 'evolution-api.client');
128    }
129
130    /**
131     * Register the main Evolution service.
132     */
133    protected function registerService(): void
134    {
135        $this->app->singleton(EvolutionService::class, function ($app) {
136            return new EvolutionService(
137                $app->make(EvolutionClientInterface::class),
138                config('evolution-api.default_instance'),
139                config('evolution-api.queue', [])
140            );
141        });
142
143        $this->app->alias(EvolutionService::class, 'evolution-api');
144    }
145
146    /**
147     * Register the webhook processor.
148     */
149    protected function registerWebhookProcessor(): void
150    {
151        $this->app->singleton(WebhookProcessor::class, function ($app) {
152            return new WebhookProcessor(
153                $app['events'],
154                $app['log']
155            );
156        });
157
158        $this->app->alias(WebhookProcessor::class, 'evolution-api.webhook');
159    }
160
161    /**
162     * Publish the configuration file.
163     */
164    protected function publishConfig(): void
165    {
166        $this->publishes([
167            __DIR__.'/../config/evolution-api.php' => config_path('evolution-api.php'),
168        ], 'evolution-api-config');
169    }
170
171    /**
172     * Publish the database migrations.
173     */
174    protected function publishMigrations(): void
175    {
176        $this->publishes([
177            __DIR__.'/../database/migrations/' => database_path('migrations'),
178        ], 'evolution-api-migrations');
179
180        // Optionally load migrations automatically if enabled
181        if (config('evolution-api.database.enabled', true)) {
182            $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
183        }
184    }
185
186    /**
187     * Register Artisan commands.
188     */
189    protected function registerCommands(): void
190    {
191        if ($this->app->runningInConsole()) {
192            $this->commands([
193                InstallCommand::class,
194                HealthCheckCommand::class,
195                InstanceStatusCommand::class,
196                PruneOldDataCommand::class,
197                RetryFailedMessagesCommand::class,
198            ]);
199        }
200    }
201
202    /**
203     * Register webhook routes.
204     */
205    protected function registerRoutes(): void
206    {
207        if (config('evolution-api.webhook.enabled', true)) {
208            $this->loadRoutesFrom(__DIR__.'/../routes/evolution-api.php');
209        }
210    }
211
212    /**
213     * Register event listeners.
214     */
215    protected function registerEventListeners(): void
216    {
217        // Event listeners will be registered here
218        // They can be overridden in the application's EventServiceProvider
219    }
220
221    /**
222     * Get the services provided by the provider.
223     *
224     * @return array<string>
225     */
226    public function provides(): array
227    {
228        return [
229            'evolution-api',
230            'evolution-api.client',
231            'evolution-api.webhook',
232            EvolutionService::class,
233            EvolutionClientInterface::class,
234            EvolutionClient::class,
235            ConnectionManager::class,
236            RateLimiterInterface::class,
237            RateLimiter::class,
238            EvolutionApiLogger::class,
239            MetricsCollector::class,
240            WebhookProcessor::class,
241        ];
242    }
243}